package com.ruoyi.count.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.count.service.IPatientStatisticsService;
import com.ruoyi.count.domain.PatientAgeStatistics;
import com.ruoyi.count.mapper.PatientStatisticsMapper;

@Service
public class PatientStatisticsServiceImpl implements IPatientStatisticsService {

    @Autowired
    private PatientStatisticsMapper patientStatisticsMapper;

    @Override
    public List<PatientAgeStatistics> getAgeStatistics(String startDate, String endDate) {
        List<PatientAgeStatistics> statistics = patientStatisticsMapper.selectAgeStatistics(startDate, endDate);
        
        // 如果数据库返回为空，初始化默认数据
        if (statistics == null || statistics.isEmpty()) {
            statistics = initializeDefaultAgeRanges();
        }
        
        // 设置序号
        for (int i = 0; i < statistics.size(); i++) {
            statistics.get(i).setIndex(i + 1);
        }
        
        return statistics;
    }
    
    private List<PatientAgeStatistics> initializeDefaultAgeRanges() {
        List<PatientAgeStatistics> defaultStats = new ArrayList<>();
        String[] ageRanges = {"0-10岁", "11-20岁", "21-30岁", "31-40岁", "41-50岁", 
                             "51-60岁", "61-70岁", "71-80岁", "80岁以上"};
        
        for (String range : ageRanges) {
            PatientAgeStatistics stat = new PatientAgeStatistics();
            stat.setAgeRange(range);
            stat.setCount(0);
            defaultStats.add(stat);
        }
        
        return defaultStats;
    }
} 